package com.tihi.handsuiviewer;

import ofs.ahd.dii.br.AdSize;
import ofs.ahd.dii.br.AdView;
import ofs.ahd.dii.br.AdViewListener;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.tihi.treeview.UITreeFrg;
import com.tihi.ui.base.BaseActivity;
import com.tihi.uiautomator.UIMarkView;
import com.tihi.util.AppUtils;
import com.tihi.util.Preferences;
import com.tihi.xml.ViewNode;
import com.unnamed.b.atv.model.TreeNode;

/**
 * UI 解析界面
 * @author duantihi
 *
 * @date 2016-2-1 上午9:37:57
 */
public class UIAnaActivity extends BaseActivity implements OnClickListener{
	private ImageView screenCap;
	private UIMarkView uiMarkView;
	private ImageView uiTreeCtl;
	private View uiTreeContainer;
	
	//ad view
	private LinearLayout adLayout;
	private Button closeAdBtn;
	
	private UIAnaLogic logic;
	
	private UITreeFrg uiTreeFrg;
	
	/**
	 * 跳转到分析UI xml界面
	 */
	public static void startAnaActivity(Context context) {
		Intent intent = new Intent(context, UIAnaActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(intent);
	}

	@Override
	protected int getLayout() {
		return R.layout.ui_ana_activity;
	}

	@Override
	protected void initViews() {
		screenCap = (ImageView) findViewById(R.id.screenShot);
		uiMarkView = (UIMarkView) findViewById(R.id.markView);
		uiTreeContainer = findViewById(R.id.uiTreeContainer);
		uiTreeCtl = (ImageView) findViewById(R.id.uiTreeCtl);
		uiTreeCtl.setOnClickListener(this);
		
		adLayout = (LinearLayout)findViewById(R.id.adLayout);
		closeAdBtn = (Button) findViewById(R.id.closeBtn);
		closeAdBtn.setOnClickListener(this);
		
//		if(showAd()) {
//			addYouMiAd();
//		}
		
		initParams();
	}
	
	/**
	 * 一天展示一次，点击过一次才显示 closeBtn
	 * @return
	 */
	private boolean showAd() {
		return true;
//		return Preferences.instance().adShowBeforeOneDay();
	}
	
	private void addYouMiAd() {
		// 实例化广告条
		AdView adView = new AdView(this, AdSize.FIT_SCREEN);
		// 将广告条加入到布局中
		adLayout.addView(adView);
		Preferences.instance().saveAdShowCurTime();
		adView.setAdListener(getAdViewListener());
	}
	
	private AdViewListener getAdViewListener() {
		return new AdViewListener() {
			
			@Override
			public void onSwitchedAd(AdView arg0) {
			}
			
			@Override
			public void onReceivedAd(AdView arg0) {
//				AppUtils.toast("onReceivedAd");
				closeAdBtn.setVisibility(View.VISIBLE);
			}
			
			@Override
			public void onFailedToReceivedAd(AdView arg0) {
				AppUtils.toast("onFailedToReceivedAd");
			}
		};
	}
	
	private void initParams() {
		logic = new UIAnaLogic(UIAnaActivity.this, uiMarkView);
		onScreenShotSuc();
		
		addFrg();
		
	}
	
	private void addFrg() {
		uiTreeFrg = new UITreeFrg();
		getSupportFragmentManager().beginTransaction()
			.replace(R.id.uiTreeContainer, uiTreeFrg)
			.commitAllowingStateLoss();
	}
	
	/**
	 * 截屏成功
	 */
	public void onScreenShotSuc() {
		screenCap.setImageBitmap(BitmapFactory.decodeFile(AppDef.SCREEN_CAP_PATH));
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		ViewNode viewNode = uiTreeFrg.getXmlParser().getViewNodeByClick(event.getX(), event.getY());
		logic.onScreenTouch(event, viewNode);
		uiTreeFrg.expandTreeNode(viewNode);
		return super.onTouchEvent(event);
	}
	
	/**
	 * ui tree节点被点击
	 * @param node
	 * @param value
	 */
	public void onTreeNodeClick(TreeNode node, Object value) {
		logic.onTreeNodeClick(node, value);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.screenShot:
			break;
		case R.id.uiTreeCtl:
			if(uiTreeContainer.getVisibility() == View.VISIBLE) {
				uiTreeContainer.setVisibility(View.GONE);
				uiTreeCtl.setImageResource(R.drawable.jiahao);
			}else {
				uiTreeContainer.setVisibility(View.VISIBLE);
				uiTreeCtl.setImageResource(R.drawable.jianhao);
			}
			break;
		case R.id.closeBtn:
			adLayout.removeAllViews();
			adLayout.setVisibility(View.GONE);
			closeAdBtn.setVisibility(View.GONE);
			break;
			
		default:
			break;
		}
	}

	@Override
	protected String getTagForUmeng() {
		return "UItree界面";
	}

}
